package com.exit490.cfp.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.exit490.cfp.R;
import com.exit490.cfp.dao.CapitalDAO;
import com.exit490.cfp.dao.CategoriaDAO;
import com.exit490.cfp.modelo.Transacao;

public class TransacaoAdapter extends ArrayAdapter<Transacao> {

    public TransacaoAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    public TransacaoAdapter(Context context, int resource, ArrayList<Transacao> listaTransacao) {
        super(context, resource, listaTransacao);
    }

    public TransacaoAdapter(FragmentActivity context, int resource, ArrayList<Transacao> listaTransacao) {
        super(context, resource, listaTransacao);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = convertView;

        if (view == null) {

            LayoutInflater layoutInflater;
            layoutInflater = LayoutInflater.from(getContext());
            view = layoutInflater.inflate(R.layout.adapter_transcao, null, false);

        }
        
        Transacao transacao = getItem(position);

        if (transacao != null) {

            TextView textViewCapital = (TextView) view.findViewById(R.id.textview_nome_conta);
            TextView textViewCategoria = (TextView) view.findViewById(R.id.textview_nome_categoria);
            TextView textViewData = (TextView) view.findViewById(R.id.textview_data_transacao);
            TextView textViewValorTransacao = (TextView) view.findViewById(R.id.textview_valor_transcao);

            CapitalDAO capitalDAO = new CapitalDAO(getContext());
            String nomeCapital = capitalDAO.selecionarNomeCapital(transacao.getIdCapital());
            textViewCapital.setText(nomeCapital);

            CategoriaDAO categoriaDAO = new CategoriaDAO(getContext());
            String nomeCategoria = categoriaDAO.selectNomeGrupo(transacao.getIdCategoria());
            textViewCategoria.setText(nomeCategoria);

            textViewData.setText(transacao.getDataTransacao());
            textViewValorTransacao.setText(String.valueOf(transacao.getValorTransacao()));

        }

        return view;
    }

}
